<template>
  <div class="container" v-loading="isLoading">
    <el-card>
      <template #header>
        <div class="table_tool">
          <el-button
            type="primary"
            icon="el-icon-plus"
            @click="showAddOrSovle = true"
            >新增</el-button
          >
        </div>
      </template>


      <el-table :data="tableData" border>
        <el-table-column prop="used" label="资金用途" />
        <el-table-column prop="price" label="金额" />
        <el-table-column prop="subdistrict_id" label="所属小区">
          <template slot-scope="{ row }">
            <div>{{communityTexts[row.subdistrict_id-1]}}</div>
          </template>
        </el-table-column>
        <el-table-column prop="create_user" label="申请人" />
        <el-table-column label="操作" width="240px" align="center">
          <template slot-scope="{ row }">
            <el-button type="text" @click="checkDetail(row.id)"
              >查看详情</el-button
            >
            <el-button type="text" @click="editDetail(row.id)"
              >编辑信息</el-button
            >
            <el-button type="text" class="red" @click="deleteById(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <footer class="footer">
      <el-pagination
        background
        layout="total, sizes, pager, jumper"
        :current-page="searchForm.page"
        :page-sizes="[10, 20, 30, 50, 100]"
        :page-size="searchForm.per_page"
        :total="tableTotal"
        @size-change="changeSize"
        @current-change="changeCurrent"
      />
    </footer>

    <!-- 查看详情 -->
    <el-dialog
      title="资金详情"
      :visible.sync="showAddOrEdit"
      width="1400px"
      @closed="closeDialog"
    >
      <el-form
        :model="builingForm"
        ref="builingForm"
        label-width="8em"
      >
        <el-form-item prop="$id" hidden />

        <el-form-item class="span-2" label="资金用途" prop="used">
          <el-input readonly
            v-model="builingForm.used"
            placeholder="资金用途"
          ></el-input>
        </el-form-item>

        <el-form-item class="span-2" label="金额" prop="price">
          <el-input readonly
            v-model="builingForm.price"
            placeholder="金额"
          ></el-input>
        </el-form-item>

        <el-form-item class="span-2" label="流程状态" prop="status">
          <el-input readonly
            v-model="builingForm.status"
            placeholder="流程状态"
          ></el-input>
        </el-form-item>

        <el-form-item class="span-2" label="所属小区" prop="status">
          <el-select disabled v-model="builingForm.subdistrict_id"  placeholder="请选择状态">
            <el-option
              v-for="(oVal, oIdx) of communityList"
              :key="oIdx"
              v-bind="oVal"
            />
          </el-select>
        </el-form-item>

        <el-form-item class="span-2" label="数据上传时间" prop="update_time">
          <el-input readonly
            v-model="builingForm.update_time"
            placeholder="数据上传时间"
          ></el-input>
        </el-form-item>

        <el-divider></el-divider>

        <el-form-item class="span-2" label="申请人姓名" prop="create_user">
          <el-input readonly
            v-model="builingForm.create_user"
            placeholder="申请人姓名"
          ></el-input>
        </el-form-item>

        <el-form-item class="span-2" label="申请人手机号" prop="create_user_phone">
          <el-input readonly
            v-model="builingForm.create_user_phone"
            placeholder="申请人手机号"
          ></el-input>
        </el-form-item>

        <el-divider></el-divider>

        <el-form-item class="span-2" label="采购单位" prop="organizer">
          <el-input readonly
            v-model="builingForm.organizer"
            placeholder="采购单位"
          ></el-input>
        </el-form-item>

        <el-form-item class="span-2" label="采购方式" prop="way">
          <el-input readonly
            v-model="builingForm.way"
            placeholder="采购方式"
          ></el-input>
        </el-form-item>

        <el-form-item label="资产备注" prop="content">
          <el-input readonly
            v-model="builingForm.content"
            type="textarea"
            row="6"
            placeholder="请输入资产备注"
          ></el-input>
        </el-form-item>

        <el-form-item label="图片" prop="imgs">
          <Upload
            v-model="builingForm.imgs"
            :file.sync="builingForm.imgs"
          />
        </el-form-item>
      </el-form>

      <template #footer>
        <el-button type="primary" @click="closeDialog()"
          >关闭</el-button
        >
      </template>
    </el-dialog>

    <!-- 编辑/新增 -->
    <el-dialog
      :title="builingForm.$id ? '编辑资金' : '新增资金'"
      :visible.sync="showAddOrSovle"
      width="1400px"
      @closed="closeDialog"
    >
      <el-form
        :model="builingForm"
        ref="builingForm"
        label-width="8em"
      >
        <el-form-item prop="$id" hidden />

        <el-form-item class="span-2" label="资金用途" prop="used">
          <el-input
            v-model="builingForm.used"
            placeholder="资金用途"
          ></el-input>
        </el-form-item>

        <el-form-item class="span-2" label="金额" prop="price">
          <el-input
            v-model="builingForm.price"
            placeholder="金额"
          ></el-input>
        </el-form-item>

        <el-form-item class="span-2" label="流程状态" prop="status">
          <el-input
            v-model="builingForm.status"
            placeholder="流程状态"
          ></el-input>
        </el-form-item>

        <el-form-item class="span-2" label="所属小区" prop="status">
          <el-select v-model="builingForm.subdistrict_id"  placeholder="请选择状态">
            <el-option
              v-for="(oVal, oIdx) of communityList"
              :key="oIdx"
              v-bind="oVal"
            />
          </el-select>
        </el-form-item>


        <el-divider></el-divider>

        <el-form-item class="span-2" label="申请人姓名" prop="create_user">
          <el-input
            v-model="builingForm.create_user"
            placeholder="申请人姓名"
          ></el-input>
        </el-form-item>

        <el-form-item class="span-2" label="申请人手机号" prop="create_user_phone">
          <el-input
            v-model="builingForm.create_user_phone"
            placeholder="申请人手机号"
          ></el-input>
        </el-form-item>

        <el-divider></el-divider>

        <el-form-item class="span-2" label="采购单位" prop="organizer">
          <el-input
            v-model="builingForm.organizer"
            placeholder="采购单位"
          ></el-input>
        </el-form-item>

        <el-form-item class="span-2" label="采购方式" prop="way">
          <el-input
            v-model="builingForm.way"
            placeholder="采购方式"
          ></el-input>
        </el-form-item>

        <el-form-item label="资产备注" prop="content">
          <el-input
            v-model="builingForm.content"
            type="textarea"
            row="6"
            placeholder="请输入资产备注"
          ></el-input>
        </el-form-item>

        <el-form-item label="图片" prop="imgs">
          <Upload
            v-model="builingForm.imgs"
            :file.sync="builingForm.imgs"
          />
        </el-form-item>
      </el-form>

      <template #footer>
        <el-button type="primary" @click="solveOrder()"
          >提交</el-button
        >
      </template>
    </el-dialog>

  </div>
</template>

<script>
import API from "@/api";
import { table, addOrEdit } from "@/utils/mixins";
import Upload from "@/components/Upload/SingleImage.vue";
export default {
  mixins: [table, addOrEdit],
    components: {
    Upload,
  },
  data: () => ({
    isLoading:true,
    isCheckDetail:false,
    showAddOrEdit:false,//查看详情弹窗
    showAddOrSovle:false,//处理弹窗
    fullscreenLoading: false,//全局loading
    tableData:[],
    searchForm:{
      page:1,
      per_page:10,
    },
    builingForm: {
      $id: "",
      used:"",
      price:'',
      way:"",
      create_user:'',
      create_user_phone:"",
      subdistrict_id:"",
      content:'',
      update_time:'',
      organizer:"",
      imgs:"",
      status:''
    },
    communityTexts:[],
    communityList:[]
  }),

  created() {
    this.getList()
    API.getCommunityList().then(({data})=>{
      this.communityTexts=data.items.map(item=>{
        return item.name
      })
      this.communityList=data.items.map(item=>{
        return {label:item.name,value:item.id}
      })

    })
  },

  methods: {
    // 获取资金列表
    getList(){
      this.isLoading=true
      API.getAssetList(this.searchForm)
      .then(({ data })=>{
        console.log(data)
        this.tableData=data.items
        this.tableTotal=data.total
      })
      .catch(()=>{
        this.tableData=[]
        this.tableTotal=0
      })
      .finally(()=>{
        this.isLoading=false
      })
    },

    // 分页
    changeSize(val){
      this.searchForm.per_page = val
      this.searchForm.page = 1
      this.getList()
    },

    // 分页
    changeCurrent(val){
      this.searchForm.page = val
      this.getList()
    },

    // 查看资金详情
    checkDetail(id){
      let _that=this
      let loadingInstance=this.$loading({ fullscreen: true })
      API.getAssetDetail(id).then(({data})=>{
        console.log(data)
        _that.builingForm.$id=data.id
        _that.builingForm.used=data.used
        _that.builingForm.price=data.price
        _that.builingForm.subdistrict_id=data.subdistrict_id
        _that.builingForm.update_time=data.update_time
        _that.builingForm.create_user=data.create_user
        _that.builingForm.create_user_phone=data.create_user_phone
        _that.builingForm.organizer=data.organizer
        _that.builingForm.way=data.way
        _that.builingForm.content=data.content
        _that.builingForm.imgs=data.imgs
        _that.builingForm.status=data.status
      }).catch(err=>{}).finally(()=>{
        this.showAddOrEdit=true
        loadingInstance.close()
      })
    },

    // 删除资金
    deleteById(id){
      let _that=this
      this.$confirm(`确定要删除这条资产信息？`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      })
        .then(() => API.delAsset(id))
        .then(() => {
          this.$notify({
            message: `删除成功`,
            type: "success",
          });
          _that.getList()
        })
        .catch(({ code }) => {
          if (code === 11100) {
            this.$message({
              iconClass: " ",
              message: "删除失败",
            });
            _that.getList()
          }
        });

    },

    // 编辑信息
    editDetail(id){
      let _that=this
      let loadingInstance=this.$loading({ fullscreen: true })
      API.getAssetDetail(id).then(({data})=>{
        _that.builingForm.$id=data.id
        _that.builingForm.used=data.used
        _that.builingForm.price=data.price
        _that.builingForm.subdistrict_id=data.subdistrict_id
        _that.builingForm.create_user=data.create_user
        _that.builingForm.create_user_phone=data.create_user_phone
        _that.builingForm.organizer=data.organizer
        _that.builingForm.way=data.way
        _that.builingForm.content=data.content
        _that.builingForm.status=data.status
        _that.builingForm.imgs=data.imgs
      }).catch(err=>{}).finally(()=>{
        this.showAddOrSovle=true
        loadingInstance.close()
      })
    },

    // 处理：新增/编辑
    solveOrder(){
      let _that=this
      let data={
        used:this.builingForm.used,
        price:this.builingForm.price,
        subdistrict_id:this.builingForm.subdistrict_id,
        create_user:this.builingForm.create_user,
        create_user_phone:this.builingForm.create_user_phone,
        organizer:this.builingForm.organizer,
        way:this.builingForm.way,
        content:this.builingForm.content,
        imgs:this.builingForm.imgs,
        status:this.builingForm.status
      }

      if(!this.builingForm.used.trim()) return this.$message.error("请填资金用途~~")
      if(!this.builingForm.price.trim()) return this.$message.error("请填写金额~~")
      if(!this.builingForm.status.trim()) return this.$message.error("请填写流程状态~~")
      if(!this.builingForm.subdistrict_id) return this.$message.error("请填写所属小区~~")
      if(!this.builingForm.create_user.trim()) return this.$message.error("请填写申请人姓名~~")
      if(!this.builingForm.create_user_phone.trim()) return this.$message.error("请填写申请人手机号~~")
      if(!this.builingForm.organizer.trim()) return this.$message.error("请填写采购单位~~")
      if(!this.builingForm.way.trim()) return this.$message.error("请填写采购方式~~")
      if(!this.builingForm.content.trim()) return this.$message.error("请填写资产描述~~")

      if(this.builingForm.$id){
        API.editAsset(this.builingForm.$id,data).then(({data})=>{
          this.$message.success("编辑成功")
          _that.getList()})
          _that.closeDialog()
      }else{
        API.addAsset(data).then(({data})=>{
          this.$message.success("新增成功")
          _that.getList()
          _that.closeDialog()
        })
      }
    },

    // 关闭弹窗
    closeDialog(){
      this.showAddOrEdit=false
      this.showAddOrSovle=false
      this.restForm()
    },

    restForm(){
      let _that=this
      _that.builingForm.$id=""
      _that.builingForm.used=""
      _that.builingForm.price=""
      _that.builingForm.subdistrict_id=1

      _that.builingForm.create_user=""
      _that.builingForm.create_user_phone=""

      _that.builingForm.organizer=""
      _that.builingForm.way=""
      _that.builingForm.content=""
    },

  },
};
</script>

<style lang="scss" scoped>
   ::v-deep{
    .el-dialog{
      margin-top:2vh !important;
    }
  }
</style>
